package com.kaka.bus.engine.home.info.message;

import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.bean.MessageBean;
import com.kaka.bus.di.DataManager;
import com.kaka.bus.manager.Result;
import com.kaka.bus.manager.T;
import com.kaka.bus.mvp.RxPresenter;

import java.util.List;

import javax.inject.Inject;

/**
 * Created by zhangqi on 2017/12/25.
 * e-mail : ${email}
 * desc :
 */

public class MessageListPresenter extends RxPresenter<MessageListContract.View> implements MessageListContract.Presenter {

    @Inject
    public MessageListPresenter(DataManager dataManager) {
        super(dataManager);
    }


    @Override
    public void initData(String userId,String token,int page, int pageSize) {
        request(userId,token,page,pageSize,false);
    }

    @Override
    public void loadMore(String userId, String token,int page, int pageSize) {
        request(userId,token,page,pageSize,true);
    }

    private void request(String userId, String token, int page, int pageSize, boolean isLoadMore){
        addSubscribe(mDataManager.getApi()
                .getMessageByUserId(userId,page,pageSize,token)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<List<MessageBean>>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<List<MessageBean>> data) {
                        if(isLoadMore){
                            mView.onResultLoadMore(data);
                        }else{
                            mView.onResultStart(data);
                        }
                    }

                    @Override
                    protected void onFaild() {
                        if(isLoadMore){
                            mView.onError();
                        }
                    }
                }));
    }

    @Override
    public void updateMessage(String userId, int id,String token, int position) {

        addSubscribe(mDataManager
                .getApi()
                .updateMessageStatus(userId,String.valueOf(id),token)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<String>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<String> stringHttpWrapper) {
                        mView.updateMessageSuccess(stringHttpWrapper,position);
                    }
                }));
    }
}
